﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using JeffWilcox.Utilities.Silverlight;
using System.Windows.Browser;
using System.Threading;
using System.IO;
using System.Text;
using System.Runtime.Serialization.Json;
using System.Reflection;
using VkApi.DataTypes;

namespace VkApi
{
    public partial class VkApi
    {
        /// <summary>
        /// Gets the variable by key.
        /// </summary>
        /// <param name="key">The key. Variable ID.</param>
        /// <param name="user_id">The user ID.</param>
        /// <param name="session">The session ID.</param>
        /// <param name="callback">The callback.</param>
        /// <returns>Variable value.</returns>
        public long App_GetVariable(int key, long? user_id, long? session, ApiCallback<long> callback)
        {
            List<Parameter> list = new List<Parameter>();
            list.Add(new Parameter("key", key.ToString()));
            if (user_id != null) list.Add(new Parameter("user_id", user_id.ToString()));
            if (session != null) list.Add(new Parameter("session", session.ToString()));         
            return this.SendRequest("getVariable", callback, list.ToArray());
        }

        /// <summary>
        /// Gets the variables.
        /// </summary>
        /// <param name="key">The key. First variable ID.</param>
        /// <param name="count">The count of variables. From 1 to 32.</param>
        /// <param name="user_id">The user ID.</param>
        /// <param name="session">The session ID.</param>
        /// <param name="callback">The callback.</param>
        /// <returns>Array of VarInfo.</returns>
        public long App_GetVariables(int key, int count, long? user_id, long? session, ApiCallback<VarInfo[]> callback)
        {
            List<Parameter> list = new List<Parameter>();
            list.Add(new Parameter("key", key.ToString()));
            list.Add(new Parameter("count", count.ToString()));
            if (user_id != null) list.Add(new Parameter("user_id", user_id.ToString()));
            if (session != null) list.Add(new Parameter("session", session.ToString()));
            return this.SendRequest("getVariables", callback, list.ToArray());
        }

        /// <summary>
        /// Stores the variable.
        /// </summary>
        /// <param name="key">The key. Variable ID.</param>
        /// <param name="value">The value.</param>
        /// <param name="user_id">The user ID.</param>
        /// <param name="session">The session ID.</param>
        /// <param name="callback">The callback.</param>
        /// <returns>Empty answer if success.</returns>
        public long App_PutVariable(int key, long value, long? user_id, long? session, ApiCallback<VarInfo[]> callback)
        {
            List<Parameter> list = new List<Parameter>();
            list.Add(new Parameter("key", key.ToString()));
            list.Add(new Parameter("value", value.ToString()));
            if(user_id!=null) list.Add(new Parameter("user_id", user_id.ToString()));
            if (session != null) list.Add(new Parameter("session", session.ToString()));
            return this.SendRequest("putVariable", callback, list.ToArray());
        }

        /// <summary>
        /// Gets the high scores table.
        /// </summary>
        /// <param name="callback">The callback.</param>
        /// <returns>Array of score info.</returns>
        public long App_GetHighScores(ApiCallback<ScoreInfo[]> callback)
        {
            return this.SendRequest("getHighScores", callback, null);
        }

        /// <summary>
        /// Sets the user score. Demo version.
        /// </summary>
        /// /// <param name="score">The user score.</param>
        /// <param name="callback">The callback.</param>
        /// <returns>Empty if success.</returns>
        public long App_SetUserScore(long score,ApiCallback<int> callback)
        {
            List<Parameter> list = new List<Parameter>();
            list.Add(new Parameter("score", score.ToString()));
            return this.SendRequest("setUserScore", callback, list.ToArray());
        }

        /// <summary>
        /// Gets the messages.
        /// </summary>
        /// <param name="messages_to_get">The messages count.</param>
        /// <param name="session">The session ID.</param>
        /// <param name="callback">The callback.</param>
        /// <returns>Array of Message Info.</returns>
        public long App_GetMessages(int? messages_to_get, long? session, ApiCallback<MessageInfo[]> callback)
        {
            List<Parameter> list = new List<Parameter>();
            if (messages_to_get != null) list.Add(new Parameter("messages_to_get", messages_to_get.ToString()));
            if (session != null) list.Add(new Parameter("session", session.ToString()));
            return this.SendRequest("getMessages", callback, list.ToArray());
        }

        /// <summary>
        /// Sends the message.
        /// </summary>
        /// <param name="messages">The messages to send.</param>
        /// <param name="session">The session ID.</param>
        /// <param name="callback">The callback.</param>
        /// <returns>Empty if success.</returns>
        public long App_SendMessage(string messages, long? session, ApiCallback<int> callback)
        {
            List<Parameter> list = new List<Parameter>();
            list.Add(new Parameter("messages", messages));
            if (session != null) list.Add(new Parameter("session", session.ToString()));
            return this.SendRequest("sendMessage", callback, list.ToArray());
        }

        /// <summary>
        /// Gets the server time in Unixtime. Wrapper for getVariable(0).
        /// </summary>
        /// <param name="callback">The callback.</param>
        /// <returns>Variable value.</returns>
        public long App_GetServerTime(ApiCallback<string> callback)
        {
            return this.SendRequest("getServerTime", callback, null);
        }

        /// <summary>
        /// Gets the targeted ads for current user.
        /// </summary>
        /// <param name="count">The count of ads to get.</param>
        /// <param name="type">The type of ads. 1 - targeted only, 2 - direct ads, 3 - all ads. 3 by default.</param>
        /// <param name="apps_id">The application IDs for direct ads.</param>
        /// <param name="min_price">The minimum price of link.</param>
        /// <param name="callback">The callback.</param>
        /// <returns>Array of Ads.</returns>
        public long App_GetAds(int? count, int? type, long?[] apps_id, int? min_price, ApiCallback<Ads[]> callback)
        {
            List<Parameter> list = new List<Parameter>();
            if (count != null) list.Add(new Parameter("count", count.ToString()));
            if (type != null) list.Add(new Parameter("type", type.ToString()));
            if (apps_id != null) list.Add(new Parameter("apps_id", apps_id.ToString()));
            if (min_price != null) list.Add(new Parameter("session", min_price.ToString()));
            return this.SendRequest("getAds", callback, list.ToArray());
        }

        /// <summary>
        /// Sets the name of application in the left menu of users page.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="callback">The callback.</param>
        /// <returns>1 if success.</returns>
        public long App_SetNameInMenu(string name, ApiCallback<int> callback)
        {
            List<Parameter> list = new List<Parameter>();
            list.Add(new Parameter("name", name));           
            return this.SendRequest("setNameInMenu", callback, list.ToArray());
        }


    }
}
